summaryrefslogtreecommitdiff
path: root/app/[lng]/test/table/page.tsx
blob: 88d050fca181930442b4685c4536b130a1050b8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
"use client"

import * as React from "react"
import { ColumnDef } from "@tanstack/react-table"
import { ClientVirtualTable } from "@/components/client-table/client-virtual-table"
import { Badge } from "@/components/ui/badge"
import { Button } from "@/components/ui/button"

// 1. Define the data type
type TestData = {
  id: string
  name: string
  email: string
  role: "Admin" | "User" | "Guest"
  status: "Active" | "Inactive" | "Pending"
  lastLogin: string
  amount: number
}

// 2. Generate dummy data
const generateData = (count: number): TestData[] => {
  const roles: TestData["role"][] = ["Admin", "User", "Guest"]
  const statuses: TestData["status"][] = ["Active", "Inactive", "Pending"]
  
  return Array.from({ length: count }).map((_, i) => ({
    id: `ID-${i + 1}`,
    name: `User ${i + 1}`,
    email: `user${i + 1}@example.com`,
    role: roles[Math.floor(Math.random() * roles.length)],
    status: statuses[Math.floor(Math.random() * statuses.length)],
    lastLogin: new Date(Date.now() - Math.floor(Math.random() * 10000000000)).toISOString().split('T')[0],
    amount: Math.floor(Math.random() * 10000),
  }))
}

export default function TestTablePage() {
  // State for data
  const [data, setData] = React.useState<TestData[]>([])
  const [isLoading, setIsLoading] = React.useState(true)

  // Load data on mount
  React.useEffect(() => {
    const timer = setTimeout(() => {
      setData(generateData(100000)) // Generate 1000 rows
      setIsLoading(false)
    }, 500)
    return () => clearTimeout(timer)
  }, [])

  // 3. Define columns
  const columns: ColumnDef<TestData>[] = [
    {
      accessorKey: "id",
      header: "ID",
      size: 80,
    },
    {
      accessorKey: "name",
      header: "Name",
      size: 150,
    },
    {
      accessorKey: "email",
      header: "Email",
      size: 200,
    },
    {
      accessorKey: "role",
      header: "Role",
      size: 100,
      cell: ({ getValue }) => {
        const role = getValue() as string
        return (
          <Badge variant={role === "Admin" ? "default" : "secondary"}>
            {role}
          </Badge>
        )
      }
    },
    {
      accessorKey: "status",
      header: "Status",
      size: 100,
      cell: ({ getValue }) => {
        const status = getValue() as string
        let color = "bg-gray-500"
        if (status === "Active") color = "bg-green-500"
        if (status === "Inactive") color = "bg-red-500"
        if (status === "Pending") color = "bg-yellow-500"
        
        return (
          <div className="flex items-center gap-2">
            <div className={`w-2 h-2 rounded-full ${color}`} />
            <span>{status}</span>
          </div>
        )
      }
    },
    {
      accessorKey: "amount",
      header: "Amount",
      size: 200,
      cell: ({ getValue }) => {
        const amount = getValue() as number
        return new Intl.NumberFormat("en-US", {
          style: "currency",
          currency: "USD",
        }).format(amount)
      },
      meta: {
        align: "right"
      }
    },
    {
      accessorKey: "lastLogin",
      header: "Last Login",
      size: 120,
    },
    {
      id: "actions",
      header: "Actions",
      size: 100,
      cell: () => (
        <Button variant="ghost" size="sm">Edit</Button>
      ),
      enablePinning: true,
    }
  ]

  return (
    <div className="h-full flex flex-col p-6 space-y-4">
      <div className="flex justify-between items-center">
        <div>
          <h1 className="text-2xl font-bold tracking-tight">Virtual Table Test</h1>
          <p className="text-muted-foreground">
            Testing the ClientVirtualTable component with 1000 generated rows.
          </p>
        </div>
        <div className="flex gap-2">
            <Button onClick={() => {
                setIsLoading(true)
                setTimeout(() => {
                    setData(generateData(5000))
                    setIsLoading(false)
                }, 500)
            }}>
                Reload 5k Rows
            </Button>
        </div>
      </div>

      <div className="border rounded-lg overflow-auto h-[1000px]">
        <ClientVirtualTable
          data={data}
          columns={columns}
          height="100%"
          isLoading={isLoading}
          enablePagination={true}
          enableRowSelection={true}
          enableGrouping={true}
          onRowClick={(row) => console.log("Row clicked:", row.original)}
          enableUserPreset={true}
          tableKey="test-table"
        />
      </div>
    </div>
  )
}